#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "getfilename.h"


char* getfilename(const char *ip)
{
    char filename[32]="\0";
    strcat(filename, "./");
    strcat(filename, ip);
    return strcat(filename, ".txt");
}

void* rdata(void* arg)
{
    umask(0);
    char buf[255];
    char filename[FILENAME_MAX]="\0";
    int n = 0;
    myefd ep, *getfd, *a;
    a = (myefd *)arg;
    getfd=a->head;
    ep.c_sum=a->c_sum;
    ep.fd=a->fd;
    strcpy(ep.ip, a->ip);
    setbuf(stdout, NULL);
    strcat(filename, "./");
    strcat(filename, ep.ip);
    pthread_mutex_t mutex_fd;
    pthread_mutex_init(&mutex_fd, NULL);
    int fd = open(strcat(filename, ".txt") , O_CREAT | O_RDWR, 0644);

    if (fd < 0)
        perror("open ep.ip error");
    char name[32],subsend[128];
    int i=0,flag=0;
    bzero(name, 32);
    bzero(subsend, 128);
    bzero(buf, 255);
    write(*ep.fd, "You are ", 8);
    write(*ep.fd, a->name, strlen(a->name));
    write(*ep.fd, " at this time\n", 14);
    //ep.ip[strlen(ep.ip)]=':';
    while ((n = read(*ep.fd, buf, 255))>0)
    {
        lseek(fd, 0, SEEK_END);
        pthread_mutex_lock(&mutex_fd);
        //write(fd, ep.ip, strlen(ep.ip));
        write(fd, buf, n);
        pthread_mutex_unlock(&mutex_fd);
        if(buf[0]=='t' && buf[1]=='o')
        {
            while(buf[i+3]!=' ')
            {
                name[i]=buf[3+i];
                i++;
            }
            /*
             * 下面是一个链表循环查找
             */
            getfd=a->head;
            while(strcmp(getfd->name, name)!=0)
            {
                if(getfd->next!=NULL)
                {
                    getfd=getfd->next;
                }
                else
                {
                    printf("can't find this person online\n");
                    break;
                }
            }
            flag=1;
            i=3+strlen(name);
            while(buf[i]!='\0')
            {
                subsend[i-3]=buf[i];
                i++;
            }
            bzero(buf, 255);
            strcat(buf,"From ");
            strcat(buf, a->name);
            write(*getfd->fd, buf, sizeof(buf));
            write(*getfd->fd, ":", 1);
            write(*getfd->fd, subsend, n-3);
            write(*getfd->fd, "--------------------------------\n", 33);
            bzero(name, 32);
            bzero(subsend, 128);
            bzero(buf, 255);
            i=0;
        }
        else if(flag==1)
        {
            write(*ep.fd, "you are talk to ", 16);
            write(*ep.fd, getfd->name, sizeof(a->name));
            write(*ep.fd, ": ", 2);
            write(*getfd->fd, "From ", 5);
            write(*getfd->fd, a->name, sizeof(a->name));
            write(*getfd->fd, buf, n);
            write(*getfd->fd, "--------------------------------\n", 33);
            bzero(buf, 255);
        }
    }
    close(*ep.fd);
    free(ep.fd);
    pthread_mutex_lock(&a->mutex_count);
    *a->c_sum=*(a->c_sum)-1;
    pthread_mutex_unlock(&a->mutex_count);
    if (*(a->c_sum) == 0)
    {
        close(*a->efd);
        close(*a->sfd);
        puts("server will close!!");
        _exit(0);
    }
    return (void*)0;
}

